<?php
require_once 'database.php';
require_once '../DTO/DOANHNGHIEP_DTO.php';
class xl_doanh_nghiep extends database
{
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from doanh_nghiep limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}


    function danh_sach_theo_loai($vi_tri, $so_luong, $ma_loai, $ma_lhkd)
    {
        //$ma_loai tuong ung voi ma loai phieu trong table loai_phieu
        //$ma_lhkd trong table loai_hinh_kinh_doanh
        $vi_tri = intval($vi_tri);
        $so_luong = intval($so_luong);
        $sql = "SELECT dn . *, lhkd.ten_loai
                FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp
                WHERE dn.loai_hinh_dn = lhkd.ma
                AND pdk.ma_dn = dn.ma
                AND pdk.loai_phieu = lp.ma
                AND lp.ma = '$ma_loai'
                AND lhkd.ma = '$ma_lhkd'
                LIMIT $vi_tri, $so_luong ";

        $this->setQuery($sql);
        return $this->loadAllRow();
    }

    function danh_sach_theo_loai_all($vi_tri, $so_luong, $ma_loai)
    {
        //$ma_loai tuong ung voi ma loai phieu trong table loai_phieu
        //$ma_lhkd trong table loai_hinh_kinh_doanh
        $vi_tri = intval($vi_tri);
        $so_luong = intval($so_luong);
        $sql = "SELECT dn . *
                FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp
                WHERE dn.loai_hinh_dn = lhkd.ma
                AND pdk.ma_dn = dn.ma
                AND pdk.loai_phieu = lp.ma
                AND lp.ma = '$ma_loai'
                LIMIT $vi_tri, $so_luong ";

        $this->setQuery($sql);
        return $this->loadAllRow();
    }


	function so_luong()
	{
		$sql = "select count(*) from doanh_nghiep";
		$this->setQuery($sql);
		return $this->loadResult();
	}

    function so_luong_theo_loai($ma_loai, $ma_lhkd)
    {
        $sql = "SELECT count(dn.ma)
                FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp
                WHERE dn.loai_hinh_dn = lhkd.ma
                AND pdk.ma_dn = dn.ma
                AND pdk.loai_phieu = lp.ma
                AND lp.ma = '$ma_loai'
                AND lhkd.ma = '$ma_lhkd' ";
        $this->setQuery($sql);
        return $this->loadResult();
    }
	function xem($ma)
	{
		$ma = intval($ma);

		$sql = "select * from doanh_nghiep where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function kt_email($email)
	{
		$sql = "select * from doanh_nghiep where email = '$email'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from doanh_nghiep where ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}

    function them(DOANHNGHIEP_DTO $info)
    {
        $info->ten_dn = stripslashes($info->ten_dn);
        $info->ten_dn = addslashes($info->ten_dn);

		$info->dia_chi = stripslashes($info->dia_chi);
        $info->dia_chi = addslashes($info->dia_chi);	

        $info->dien_thoai = stripslashes($info->dien_thoai);
        $info->dien_thoai = addslashes($info->dien_thoai);

        $info->email = stripslashes($info->email);
        $info->email = addslashes($info->email);

        $info->website = stripslashes($info->website);
        $info->website = addslashes($info->website);
		
		$info->ten_giam_doc = stripslashes($info->ten_giam_doc);
        $info->ten_giam_doc = addslashes($info->ten_giam_doc);
		
		$info->ma_so_thue = stripslashes($info->ma_so_thue);
        $info->ma_so_thue = addslashes($info->ma_so_thue);
		
		$info->gioi_thieu = stripslashes($info->gioi_thieu);
        $info->gioi_thieu = addslashes($info->gioi_thieu);


        $sql = "INSERT INTO   doanh_nghiep  ( ten_dn  ,   dia_chi  ,   dien_thoai  ,   ma_so_thue  ,   email  ,   website  ,   gioi_thieu  ,   trang_thai  ,   loai_hinh_dn  ,   tinh_trang_dn  ,   gia  ,   thong_tin_rieng  ,   dia_phuong_quan_tam  )
                      VALUES   (
                                    '$info->ten_dn',
                                    '$info->dia_chi',
                                    '$info->dien_thoai',
                                    '$info->ma_so_thue',
                                    '$info->email',
                                    '$info->website',
                                    '$info->gioi_thieu',
                                    '$info->trang_thai',
                                    '$info->loai_hinh_dn',
                                    '$info->tinh_trang_dn',
                                    '$info->gia',
                                    '$info->thong_tin_rieng',
                                    '$info->dia_phuong_quan_tam'
		                      )";
        $this->setQuery($sql);
        return $this->query();
    }


	function cap_nhat($ma, $ho_ten, $ngay_sinh, $gioi_tinh, $dia_chi, $dien_thoai, $email, $vi_tri_ung_tuyen, $tu_gioi_thieu, $trang_thai)
	{
		$ma = intval($ma);
		$ho_ten = stripslashes($ho_ten);
		$ho_ten = addslashes($ho_ten);
		$dia_chi = stripslashes($dia_chi);
		$dia_chi = addslashes($dia_chi);
		$dien_thoai = stripslashes($dien_thoai);
		$dien_thoai = addslashes($dien_thoai);
		$email = stripslashes($email);
		$email = addslashes($email);
		$vi_tri_ung_tuyen = stripslashes($vi_tri_ung_tuyen);
		$vi_tri_ung_tuyen = addslashes($vi_tri_ung_tuyen);
		$tu_gioi_thieu = stripslashes($tu_gioi_thieu);
		$tu_gioi_thieu = addslashes($tu_gioi_thieu);

		$sql ="UPDATE doanh_nghiep SET ho_ten= '$ho_ten', ngay_sinh= '$ngay_sinh', gioi_tinh= '$gioi_tinh', dia_chi= '$dia_chi', dien_thoai= '$dien_thoai', email= '$email', vi_tri_ung_tuyen= '$vi_tri_ung_tuyen', tu_gioi_thieu= '$tu_gioi_thieu', trang_thai= '$trang_thai' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function tim_kiem ($tu_khoa)
	{
		//$sql = "select * from doanh_nghiep where	$ho_ten like %tu_khoa% or 	$dia_chi like %tu_khoa% or 	$dien_thoai like %tu_khoa% or 	$email like %tu_khoa% or 	$vi_tri_ung_tuyen like %tu_khoa% or 	$tu_gioi_thieu like %tu_khoa%";
		//$this->setQuery($sql);
		return $this->loadAllRow();
	}
 }
?>